﻿using BaseUtils.Helper;
using Newtonsoft.Json;
using NWMES;
using NWMES.Mes;
using NWMES.MiReleaseSfcWithActivityServiceService;
using System;
using System.Net;

namespace MyMes.Mes
{

    /// <summary>
    /// 释放总成号
    /// </summary>
    public class MiReleaseSfcWithActivity : MESBaseService
    {
        static string DefaultApiSection = "MiReleaseSfcWithActivity";
        public static NetworkCredential networkCredential = null;
        public static MiReleaseSfcWithActivity Params = null;

        static MiReleaseSfcWithActivity()
        {
            Params = config[DefaultApiSection].ToObject<MiReleaseSfcWithActivity>();
            networkCredential = new NetworkCredential(Params.UserName, Params.Password);
        }

        /// <summary>
        /// 释放（模组号）总成号
        /// </summary>
        /// <param name="ActivityCode"></param>
        /// <param name="strError"></param>
        /// <returns></returns>
        public static MesResult RequestMes(string ApiSection)
        {
            MiReleaseSfcWithActivityServiceService miReleaseSfcWithActivityServiceService;
            releaseSfcWithActivityRequest Request;
            miReleaseSfcWithActivity MiReleaseSfcWithActivity;
            if (!string.IsNullOrEmpty(ApiSection))
            {
                Params = config[ApiSection].ToObject<MiReleaseSfcWithActivity>();
                networkCredential = new NetworkCredential(Params.UserName, Params.Password);
            }
            else
            {
                ApiSection = DefaultApiSection;
            }
            string ErrorMessage = Params.ParameterVerification();
            if (!string.IsNullOrEmpty(ErrorMessage))
            {
                return new MesResult(-99, ErrorMessage);
            }
            SetRequestParams(out miReleaseSfcWithActivityServiceService, out Request, out MiReleaseSfcWithActivity);

            miReleaseSfcWithActivityResponse Response = new miReleaseSfcWithActivityResponse();
            MesResult result = MesResult.InitMesResult(JsonConvert.SerializeObject(MiReleaseSfcWithActivity));
            try
            {
                Response = miReleaseSfcWithActivityServiceService.miReleaseSfcWithActivity(MiReleaseSfcWithActivity);

                result.EndTime = DateTime.Now;
                var ret = Response.@return;
                result.Message = ret?.message;
                result.Code = ret.code;
                result.Success = result.Code == 0;
                result.ResponeJson = JsonConvert.SerializeObject(Response);

                result.Respone = Response;
                result.ret = ret;

                if (ret != null && ret.sfcArray.Length > 0)
                {
                    sfcWithLocation[] sfcWiths = ret.sfcArray;
                    string[] sfcs = new string[sfcWiths.Length];
                    for (int i = 0; i < sfcWiths.Length; i++) {
                        sfcs[i] = sfcWiths[i].sfc;
                    }
                    result.sfcArray = sfcs;
                    result.sfc = sfcs[0];
                }
            }
            catch (Exception ex)
            {
                result.Code = -99;
                result.Message = ex.Message;
            }
            SaveMesLog(ApiSection, result);
            return result;
        }

        private static void SetRequestParams(out MiReleaseSfcWithActivityServiceService miReleaseSfcWithActivityServiceService, out releaseSfcWithActivityRequest ReleaseSfcWithActivityRequest, out miReleaseSfcWithActivity MiReleaseSfcWithActivity)
        {
            miReleaseSfcWithActivityServiceService = new MiReleaseSfcWithActivityServiceService();
            miReleaseSfcWithActivityServiceService.Url = Params.Url;
            miReleaseSfcWithActivityServiceService.Timeout = Params.Timeout;
            miReleaseSfcWithActivityServiceService.Credentials = networkCredential;
            miReleaseSfcWithActivityServiceService.PreAuthenticate = true;

            ReleaseSfcWithActivityRequest = new releaseSfcWithActivityRequest();
            ReleaseSfcWithActivityRequest.site = Params.Site;
            ReleaseSfcWithActivityRequest.user = Params.User;
            ReleaseSfcWithActivityRequest.operation = Params.Operation;
            ReleaseSfcWithActivityRequest.operationRevision = Params.OperationRevision;
            ReleaseSfcWithActivityRequest.activity = Params.ActivityId;
            ReleaseSfcWithActivityRequest.resource = Params.Resource;
            ReleaseSfcWithActivityRequest.modeProcessSFC = ConvertHelper.ToEnum<modeProcessSFC>(Params.ModeProcessSFC);
            ReleaseSfcWithActivityRequest.sfcQty = Params.sfcQty;

            MiReleaseSfcWithActivity = new miReleaseSfcWithActivity();
            MiReleaseSfcWithActivity.ReleaseSfcWithActivityRequest = ReleaseSfcWithActivityRequest;
        }

    }

}
